<?php
class userClass
{
	/*
	 * this adds a new user to the database
	 * takes the username and password (should have already been sanatized)
	 * should return true if the account was created and false if it encountered an error
	 */
	static function addUser($username, $password)
	{
		$sql = "insert into users (USER_TYPE_ID, USERNAME, PASSWORD) values(1, '$username', '$password')";
		//echo $sql; die();
		return database::insertSQL($sql);
	}
	
	/*
	 * this deletes a user from the database
	 * this is only user for testing now, and there is no actual functionality in the website
	 * to delete a user
	 * 
	 * it takes a username, should be sanatized
	 * since usernames are unique, it only needs to look up the username by itself
	 */
	static function deleteUser($username)
	{
		$sql = "delete from users where USERNAME = '$username'";
		//echo $sql; die();
		return database::insertSQL($sql);
	}
	
	/*
	 * tries to login a user with the creds provided
	 * takes a username and password (should have already been sana)
	 * returns true if correct creds and false if wrong
	 */
	static function loginUser($username, $password)
	{
		$DB = database::getDB();
		
		$sql = "select USERNAME from users where USERNAME = '$username' and PASSWORD = '$password'";
		//echo $sql; die();
		$result = mysqli_query($DB, $sql);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
			$name = $row['USERNAME'];
			
		mysqli_free_result($result);
		mysqli_close($DB);
		
		if(empty($name))
			return false;
		else
			return true;
	}
	
	/*
	 * tries to get the username from an account id
	 * first checks if numeric, otherwise returns false
	 * then gets username and returns correct one 
	 * or null if an error occurred
	 */
	static function getUsernameByID($ID)
	{
		if(!is_numeric($ID))
			return false;
			
		$DB = database::getDB();
		
		$sql = "select USERNAME from users where ID = $ID";
		//echo $sql; die();
		$result = mysqli_query($DB, $sql);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
			$name = $row['USERNAME'];
			
		mysqli_free_result($result);
		mysqli_close($DB);
		
		if(empty($name))
			return null;
		else
			return $name;
	}
	
	/*
	 * returns the id for the username input
	 * username should be sanatized
	 * returns id if real username
	 * or null of not
	 */
	static function getIDByUsername($username)
	{
		$DB = database::getDB();
		
		$sql = "select ID from users where USERNAME = '$username'";
		//echo $sql; die();
		$result = mysqli_query($DB, $sql);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
			$ID = $row['ID'];
			
		mysqli_free_result($result);
		mysqli_close($DB);
		
		if(empty($ID))
			return null;
		else
			return $ID;
	}
	
	/*
	 * returns the type id for the user
	 * takes a single id
	 * returns type id, else defeults to 1 (lowest level)
	 */
	static function getUserType($ID)
	{
		$DB = database::getDB();
		
		$sql = "select USER_TYPE_ID from users where ID = $ID";
		//echo $sql; die();
		$result = mysqli_query($DB, $sql);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
			$typeID = $row['USER_TYPE_ID'];
			
		mysqli_free_result($result);
		mysqli_close($DB);
		
		if(empty($typeID))
			return 1;
		else
			return $typeID;
	}
}